#!/bin/sh

chmod 755 *.sh
chmod 755 *.cgi
chmod 755 haserl wget
chmod 666 *.conf

[ -d /conf/.cron ] || mkdir /conf/.cron
D=/conf/.cron/backup_web2wget/`date +%F_%H-%M-%S`
mkdir -p $D
for i in *.sh
do
  [ -e /conf/.cron/`basename $i` ] && cp /conf/.cron/`basename $i` $D/
done
cp -f *.sh /conf/.cron/

[ -s /conf/.cron/wget ] || cp -f wget /conf/.cron/
[ -s /conf/.cron/haserl ] || cp -f haserl /conf/.cron/
[ -e /conf/.cron/web2wget.conf ] && cp /conf/.cron/web2wget.conf $D/
cp -f web2wget.conf /conf/.cron/

[ -e /conf/.cron/wget.cgi ] && cp /conf/.cron/wget.cgi $D/
cp -f wget.cgi /web/cgi-bin/
cp -f wget.cgi /conf/.cron/

#For firmware V01R09
if [ -s /web/js/menu_en.js ]; then
    [`cat /web/js/menu_en.js | grep WGET | wc -l` -eq 0 ] && echo "insDoc(fSubSub,gLnk('R', 'WGET Downloader', '/cgi-bin/wget.cgi'))" >> /web/js/menu_en.js
fi
#For firmware V01R07
if [ -s /web/treeFramesetNodes_en.js ]; then
    [`cat /web/treeFramesetNodes_en.js | grep WGET | wc -l` -eq 0 ] && echo "insDoc(fSubSub,gLnk('R', 'WGET Downloader', '/cgi-bin/wget.cgi'))" >> /web/treeFramesetNodes_en.js
fi

if [ `cat /etc/crontab | grep get.sh | wc -l` -eq 0 ]; then
  echo "*/5 * * * * root /conf/.cron/get.sh &" >> /etc/crontab
  killall crond
  /usr/sbin/crond
fi

rm -f /conf/.cron/install.sh
